//
//  SettingsViewController.swift
//  onemeetingmac
//
//  Created by 张衡 on 2019/12/12.
//  Copyright © 2019 onemeeting. All rights reserved.
//

import Foundation
enum SetSubType {
    case `default`
    case group
    case head
    case foot
}

struct Item {
    var title : String? = ""
    var contents:[String]? = nil
    var type: SetSubType = .default
    
    init(title :String,contents: [String]? = nil,type: SetSubType = .default) {
        self.title = title
        self.contents = contents
        self.type = type
    }
}


class SettingsViewController: BaseViewController {
    let dataList = [
        
            //Item(title: "关于壹会通", type:.head),
            /*
            Item(title: "常规", type:.group),
            Item(title: "会议模式", contents: ["禁用动画效果（减少资源占用）"]),
            Item(title: "播放列表：", contents: ["双击播放单曲时，用当前单曲所在单曲替换当前（节目同理）","双击单曲时，仅当前单曲添加（节目同理）"]),
            Item(title: "播放歌曲时：", contents: ["启动系统播放栏"]),

            Item(title: "消息与隐私", type:.group),
            Item(title: "私信：接受私信提醒", contents: ["所有人","我关注的所有人"]),

            Item(title: "下载设置", type:.group),
            Item(title: "通知：", contents: ["歌单被收藏","收到赞","新粉丝"]),
            Item(title: "推荐动态：", contents: ["启动系统播放栏"]),
            Item(title: "我听歌排行：", contents: ["所有人可见","关注人可见","仅自己可见"]),
            */
            Item(title: "常规", type:.group),
            Item(title: "会议模式：", contents: ["互动模式（全部视频显示）","直播模式（仅显示主播的视频）"]),
            
        
    ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    func setupUI() {
        self.view.addSubview(tabContainerView)
        tabContainerView.snp.makeConstraints { (make) in
           make.edges.equalToSuperview().inset(NSEdgeInsets.init(top: kNavBarHeight-20, left: 0, bottom: 0, right: 0))
        }
        self.view.addSubview(backBtn)
        backBtn.snp.makeConstraints { (make) in
            make.width.height.equalTo(20)
            make.right.equalTo(-30)
            make.top.equalTo(20)
        }
    }
    lazy var backBtn: LLCustomBtn = {
        let btn = LLCustomBtn()
        btn.defaultBackgroundImage = NSImage.init(named: NSImage.Name.init("icon_navigate_updown"))
        btn.action = #selector(backBtnAction)
        btn.target = self
        return btn
    }()
    
    @objc func backBtnAction()  {
        self.dismiss(nil)
    }
    
    lazy var tabContainerView: NSScrollView = {
        let view  = NSScrollView.init()
        view.documentView = self.tabView
        view.hasHorizontalRuler = true
        view.hasVerticalRuler = true
        view.wantsLayer = true
        view.backgroundColor = NSColor.white
        view.autohidesScrollers = false
        return view
    }()
    
    lazy var tabView: NSTableView = {
        let tab = NSTableView.init()
        tab.wantsLayer = true
        tab.backgroundColor = NSColor.white
        tab.delegate = self
        tab.dataSource = self
        tab.rowSizeStyle = .large
        tab.floatsGroupRows = false
        tab.focusRingType = NSFocusRingType.none
        tab.selectionHighlightStyle = NSTableView.SelectionHighlightStyle.none
        //tab.usesAlternatingRowBackgroundColors = true
    
        let column = NSTableColumn.init(identifier: NSUserInterfaceItemIdentifier.init("column"))
        tab.addTableColumn(column)
        //column.width = self.view.frame.size.width
        tab.headerView = nil
        return tab
    }()
}

extension SettingsViewController:NSTableViewDelegate,NSTableViewDataSource {

      func numberOfRows(in tableView: NSTableView) -> Int {
          return dataList.count
      }

      
      func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
          
          let itm = dataList[row]
          switch itm.type {
          case .default:
              let cellid = "column"
              var cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: cellid), owner: self) as? SetCellView
              if cell == nil {
                  cell = SetCellView.init()
              }
              cell?.title.attributedStringValue =  (itm.title!).subColonAttributed(textColor: color_gray(210), font: NSFont.systemFont(ofSize: 12))
              
              switch row {
              case 1:
                  cell?.type = .radio
              default:
                  cell?.type = .multichoice
              }
              
              cell?.initconvens(name: itm.contents!)
              
              return cell
          case .group:
              let cellgroupid = "cellgroup"
              var cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: cellgroupid), owner: self) as? SetGroupCellView
              if cell == nil {
                  cell = SetGroupCellView.init()
              }
              cell?.isGroupRowStyle = true
              cell?.isFloating = true
              cell?.title.stringValue = itm.title!
              return cell
          case .head:
              let cellgroupid = "cellhead"
              var cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: cellgroupid), owner: self) as? SetTabHeadFootView
              if cell == nil {
                  cell = SetTabHeadFootView.init(.head)
              }
              cell?.title.stringValue = itm.title!
              return cell
          case .foot:
              print("")
          }
          return nil
      }
      
    
      func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
          switch dataList[row].type {
          case .group:
              return 60
          case .head:
              return 100
          case .foot:
              return 60
          case .default:
               return CGFloat(50+dataList[row].contents!.count*35)
          }
          
      }
      
      func tableView(_ tableView: NSTableView, shouldSelectRow row: Int) -> Bool {
          return true
      }
}
